<template lang='pug'>
.page
  .BasicDataManage
    .search-form.mb-25.pl-10.pr-10
      Row.mb-15(type="flex" justify="start")
        Col.pr-20(:md="{ span: 24 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 数据类型：
          Input(v-model="formData.typeName" placeholder="请输入")
        Col.pr-20(:md="{ span: 24 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          span.label-txt.w80 数据名称：
          Input(v-model="formData.name" placeholder="请输入")

        Col(:md="{ span: 24 }" :lg="{ span: 12 }" :xl="{ span: 8 }" :xxl="{span: 6}")
          Button(v-if="hasFind" type="info" @click="searchFn") 查询

    .pl-20.pr-20.bg-white
      Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true"
        @on-selection-change="handleOnSelectChange"
      )
        .clearfix.font-16(slot="header")
          .table-title-lefttxt.pull-left
            |合计
            span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
            |条
          .pull-right.table-title-btns
            Button.mr-10(v-if="hasCreate" type="info" @click="handleShowCreateBasicDataModal('')") 新增
            //- Button.mr-10(type="warning" @click="handleDel") 删除
            //- Button(type="info" :disabled="!selectedId.length > 0" @click="enabledList(selectedId, true)") 启用

        template(slot-scope="{ row, index }" slot="action")
          Button.mr-10(v-if="hasEdit" type="text" size="small" @click="handleShowCreateBasicDataModal(row.id)") 编辑
          Button.mr-10(type="text" size="small" v-if="!row.enabled && hasEnabledList" @click="enabledList(row.id, true)") 启用
          Button.mr-10(type="text" size="small" v-if="row.enabled && hasDisEnabledList" @click="enabledList(row.id, false)") 禁用

        .clearfix.pl-20.pr-20(slot="footer")
          Page(
            :current="tableComone.pageIndex"
            :total="tableComone.totalRows"
            @on-change="onChangePage"
            @on-page-size-change="onpageSizeChange"
            show-sizer
            show-total
            placement="top"
            prev-text="上一页"
            next-text="下一页"
            :page-size-opts="[10, 20, 30, 50, 100]"
          )

    //- 新建弹窗
    CreateBasicDataModal(ref="CreateBasicDataModal" @handleSave="searchFn")
</template>

<script>
import { SysBasedataApi } from '@/api/SysBasedata.api.js'
import { basicThead } from './BDtableHead.js'
import _cloneDeep from 'clone-deep'
import CreateBasicDataModal from './modal/CreateBasicDataModal.vue'

export default {
  name: 'BasicDataModalModal',

  components: {
    CreateBasicDataModal
  },

  data () {
    return {
      thead: _cloneDeep(basicThead),
      tableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      formData: {
        // type: '',
        typeName: '',
        name: ''
      },
      selectedId: '', // 勾选的id集合
      selectedIdArr: [] // 勾选的id, name数组集合
    }
  },

  mounted () {
    this.query()
  },
  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 查询
    hasFind () {
      return this.actionArr.indexOf('Find') > -1
    },
    // 查看
    hasView () {
      return this.actionArr.indexOf('View') > -1
    },
    // 新增
    hasCreate () {
      return this.actionArr.indexOf('Create') > -1
    },
    // 编辑
    hasEdit () {
      return this.actionArr.indexOf('Edit') > -1
    },
    // 删除
    hasDelete () {
      return this.actionArr.indexOf('Delete') > -1
    },
    // 启用
    hasEnabledList () {
      return this.actionArr.indexOf('EnabledList') > -1
    },
    // 禁用
    hasDisEnabledList () {
      return this.actionArr.indexOf('DisEnabledList') > -1
    }
  },

  methods: {
    // 数据字典
    // getEnum () {
    //   let groupName = 'change.securitytype'
    //   Common.getEnum(groupName, this.arrListDown)
    //   // console.log('数据字典：', this.arrListDown)
    // },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        TypeName: this.formData.typeName,
        Name: this.formData.name,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await SysBasedataApi.getList(params)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedIdArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      let arr = value.map((item) => {
        return item.id
      })
      this.selectedId = arr.join(',')
    },

    // 显示新增弹窗
    handleShowCreateBasicDataModal (id) {
      this.$refs.CreateBasicDataModal.show()
      // this.$refs.CreateBasicDataModal.isShowCreateBasicDataModal = true
      this.$refs.CreateBasicDataModal.createBasicDataModalTitle = id ? '修改基础数据' : '新增基础数据'
      this.$refs.CreateBasicDataModal.getModel(id)
      this.$refs.CreateBasicDataModal.basicDataId = id
      Object.keys(this.$refs.CreateBasicDataModal.formCreate).forEach(key => {
        if (key === 'isDefault') {
          this.$refs.CreateBasicDataModal.formCreate[key] = false
        } else if (key === 'enabled') {
          this.$refs.CreateBasicDataModal.formCreate[key] = true
        } else {
          this.$refs.CreateBasicDataModal.formCreate[key] = ''
        }
      })
    },

    // 启用/禁用
    async enabledList (id, enabled) {
      var params = {
        ids: id,
        enabled: enabled
      }
      let res = await SysBasedataApi.enabledList(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 删除
    async handleDel (id, enabled) {
      this.$Message.success('点击了删除')
      // var params = {
      //   ids: id,
      //   enabled: enabled
      // }
      // let res = await SysBasedataApi.enabledList(params)
      // if (res.status === 200 && res.data.success) {
      //   this.$Message.success(res.data.message)
      //   this.query()
      // } else {
      //   this.$Message.error({
      //     content: res.data.message,
      //     // duration: 0,
      //     closable: true
      //   })
      // }
    }
  }
}
</script>

<style>
</style>
